// Bhumika Udani    v1.2      dt: 20/01/2011
// Covered the Lead Status update condition in workflows

public with sharing class LeadOperation{
Public static ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');

    public static void UpdateStatus(List<Lead> Leads){
    //ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
    for(Lead lead :Leads){
        if(lead.RecordTypeId == localAdvertiserRecordTypeID){
        //update lead  source code    
            if(lead.LeadSource== 'Prospect') lead.lead_source_id__c = 'AP';
            if(lead.LeadSource== 'Salesgenie.com') lead.lead_source_id__c = 'SG';
            if(lead.LeadSource== 'Call In') lead.lead_source_id__c = 'Cl';
            if(lead.LeadSource== 'Email-In') lead.lead_source_id__c = 'EM';
            if(lead.LeadSource== 'ASCP-SEM') lead.lead_source_id__c = 'SM';
            if(lead.LeadSource== 'InfoUSA - Free Listing') lead.lead_source_id__c = 'IU';
            if(lead.LeadSource== 'Internet') lead.lead_source_id__c = Lead.BSC_source_code__c;
            system.debug('Yellow' + lead.Disposition__c);
            system.debug('Yellow1' + lead.lead_source_id__c);

 /* Bhumika Udani dt 20/01/2011 - Covered the following conditions through workflow hence commenting
           if (lead.Disposition__c == 'Called - Spoke with DM' || lead.Disposition__c == 'Called - Unavailable' || lead.Disposition__c == 'Called - Left VM' || lead.Disposition__c == 'Appt – Follow-up' || lead.Disposition__c == 'Appt - Presentation' || lead.Disposition__c == 'Appt - First Appt'){
               lead.Status = 'In Progress';
               lead.Lead_Status_ReadOnly__c = 'In Progress';}

            if (lead.Disposition__c == 'Signed Contract'){
                // lead.Status = 'Signed Contract';
                lead.Lead_Status_ReadOnly__c = 'Signed Contract';}

            if (lead.Disposition__c == 'Invalid' || lead.Disposition__c == 'No Sale' ){
                    lead.Status = 'Invalid Lead';
                    lead.Lead_Status_ReadOnly__c = 'Invalid Lead';}
                    
            if (lead.Disposition__c == 'Closed'){
                    lead.Status = 'Closed';
                    lead.Lead_Status_ReadOnly__c = 'Closed';}
 */
        }
    }
  
}

//Method to update the Primary Owner text field
public static void UpdatePrimaryOwner(List<Lead> Nleads, List<Lead> OldLeads){
    List<id> LeadOwnerIds = new List<id>();
    List<Lead> NewLeads = new List<Lead>(); 
    Map<id,id> NewMap = new Map<id,id>(); 
    Map<id,id> OldMap = new Map<id,id>();
    Map<id,String> OwnerMap = new Map<id,String>();

    for(lead ld :NLeads){
        if(ld.RecordTypeId == localAdvertiserRecordTypeID){
            NewMap.put(ld.id, ld.ownerid);
            NewLeads.add(ld);
            LeadOwnerIds.add(ld.ownerid);
        }
    }
    for(Lead Ld :OldLeads){
         if(Ld.RecordTypeId == localAdvertiserRecordTypeID){
             OldMap.put(Ld.id, Ld.ownerid);}
                }
    List<User> LeadOwners = [select id, name from user where id in :LeadOwnerIds];
       
    for(User U : LeadOwners){
                 OwnerMap.put(U.Id, U.Name);
             }
             
     ListViewIds__c LV = ListViewIds__c.getInstance();
     Id OORNewJerseyQueueId  = LV.SFA_OOR_New_Jersey_Sharkpool__c;
     Id OORSaltLakeCityQueueId = LV.SFA_OOR_Salt_Lake_Sharkpool__c;


      for(lead Nlead : NewLeads){
          if(NewMap.get(Nlead.id) != OldMap.get(Nlead.id)){
             if(NewMap.get(Nlead.id) == OORSaltLakeCityQueueId){
                 Nlead.Primary_Owner__C = 'Sharkpool - Salt Lake (SLC)';
                 Nlead.Secondary_Owner__c = null;}
             else if(NewMap.get(Nlead.id) == OORNewJerseyQueueId){
                 Nlead.Primary_Owner__C = 'Sharkpool - New Jersey (NNJ)';
                 Nlead.Secondary_Owner__c = null;}
             else
                Nlead.Primary_Owner__C =  OwnerMap.get(Nlead.ownerid);} 
            }
        }

    

// test class 

static testMethod void testLeadOp()
{
ID localLeadRecID= RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');

// creating the list of leads //

// record 1
try{
Lead ld1 = new Lead();

List<Lead> ldlist = new List<Lead>();
List<Lead> ldlist2 = new List<Lead>();


List<Lead> ldlist3 = new List<Lead>();


ld1.RecordTypeId=localLeadRecID;
ld1.Company ='Test OOR 1';
ld1.FirstName='John';
ld1.LastName='Smith';
ld1.Phone='8989898989';
ld1.Disposition__c='No Sale';
ld1.Most_Recent_Note__c='test3';
ld1.LeadSource='Salesgenie.com';

insert ld1;
ldlist.add(ld1);


// record 2

Lead ld2 = new Lead();

ld2.RecordTypeId=localLeadRecID;
ld2.Company ='Test OOR 2';
ld2.FirstName='John2';
ld2.LastName='Smith2';
ld2.Phone='89898989789';
ld2.Disposition__c='Closed';
ld2.LeadSource='Call In';

insert ld2;
ldlist3.add(ld2);

// record 3

Lead ld3 = new Lead();

ld3.RecordTypeId=localLeadRecID;
ld3.Company ='Test OOR 2';
ld3.FirstName='John2';
ld3.LastName='Smith2';
ld3.Phone='89898989789';
ld3.Disposition__c='Signed Contract';
ld3.Most_Recent_Note__c='test2';
ld3.LeadSource='Email-In';

insert ld3;
ldlist2.add(ld3);

// note insert 
Notes__c callnotes = new Notes__c();
callnotes.Lead__c=ld3.Id;
insert callnotes;

//CreateNewNote(ldlist,ldlist2);
UpdatePrimaryOwner(ldlist,ldlist);
UpdateStatus(ldlist);

}
catch(Exception e){
System.debug('Exception catch'+e);
}

}
}